﻿Public Class UsersForm
    Dim mainsql As String
    Dim Filter As Boolean


    Private Sub LoadUsersgrid()
        Job("Loading users...")
        UsersGrid.DataSource = LoadDataView(mainsql)
        UsersGrid.Refresh()
        Job("Ready")

    End Sub

    Private Sub UsersForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mainsql = "Select User_Name as [User Name], User_Level as [Level], Last_Logon as [Last Logon] from Users"
        Filter = False
        LoadUsersgrid()

    End Sub

    Private Sub Load_Activities(ByVal User_Name As String)
        Dim sql As String

        If Filter = False Then
            sql = "Select Activity_Date as [Activity Date], Activity_Description as [Activity], Type from User_Activities where User_Name = '" & UsersGrid.Item(0, UsersGrid.CurrentRow.Index).Value & "' order by Activity_date desc"
        Else
            With UserFilterDialog
                sql = "Select Activity_Date as [Activity Date], Activity_Description as [Activity], Type from User_Activities " & _
                                        "where User_Name = '" & UsersGrid.Item(0, UsersGrid.CurrentRow.Index).Value & "' " & _
                                   "and (Activity_Date >= " & .FromBox.Value.Date.ToOADate - 2 & " and Activity_Date <= " & (.ToBox.Value.Date.ToOADate - 2) + 0.9999999999 & ") order by Activity_date desc"
            End With

       End If

        UserActivitiesGrid.DataSource = LoadDataView(sql)
        UserActivitiesGrid.Refresh()
    End Sub

    Private Sub DeleteBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteBut.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to delete users." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Delete User", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        Dim UserToDelete As String

        If UsersGrid.SelectedRows.Count = 0 Then Exit Sub

        UserToDelete = UsersGrid.Item(0, UsersGrid.CurrentRow.Index).Value

        'make sure that you are not deleting the current user
        If UserToDelete = CurrentUser.Name Then
            MessageBox.Show("You cannot delete yourself", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If

        If MessageBox.Show("Are you sure you want to delete " & UserToDelete & "?", "Delete User?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            If RunNonQuery("Delete Users where User_Name = '" & UserToDelete & "'") = "" Then
                AddActivity(CurrentUser.Name, "User " & UsersDialog.UserNameBox.Text & " was deleted", 6, "")
                LoadUsersgrid()
            End If

        End If
    End Sub

    Private Sub RefreshBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RefreshBut.Click
        LoadUsersgrid()

    End Sub

    Private Sub AddBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddBut.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to add users." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Add User", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        'Reset boxes
        UsersDialog.UserNameBox.Text = ""
        UsersDialog.UserLevelBox.Text = 0
        UsersDialog.UserNameBox.Enabled = True

        If UsersDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim sql As String

            sql = "Insert into Users values('" & UsersDialog.UserNameBox.Text & "', " & UsersDialog.UserLevelBox.Text & ", null, null, null)"

            If RunNonQuery(sql) = "" Then
                AddActivity(CurrentUser.Name, UsersDialog.UserNameBox.Text & " added as new user with level " & UsersDialog.UserLevelBox.Text & " access", 4, "")
                LoadUsersgrid()
                FindUser(UsersDialog.UserNameBox.Text)

            End If
        End If
    End Sub

    Private Sub EditBut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditBut.Click

        If UserFormCheck(Me.Name) <> "W" Then
            MessageBox.Show("Currently you do not have permission to edit users." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk to change permissions.", "Edit User", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If

        'Reset boxes
        UsersDialog.UserNameBox.Text = UsersGrid.Item(0, UsersGrid.CurrentRow.Index).Value
        UsersDialog.UserLevelBox.Text = UsersGrid.Item(1, UsersGrid.CurrentRow.Index).Value

        UsersDialog.UserNameBox.Enabled = False

        If UsersDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim sql As String

            sql = "Update Users set User_Level = " & UsersDialog.UserLevelBox.Text & " where User_Name = '" & UsersDialog.UserNameBox.Text & "'"

            If RunNonQuery(sql) = "" Then
                AddActivity(CurrentUser.Name, "User " & UsersDialog.UserNameBox.Text & " access level was changed to " & UsersDialog.UserLevelBox.Text, 5, "")
                LoadUsersgrid()
                FindUser(UsersDialog.UserNameBox.Text)
            End If
        End If
    End Sub


    Private Sub FindUser(ByVal UserName As String)
        Dim r As Integer

        If UsersGrid.RowCount = 0 Then Exit Sub

        For r = 0 To UsersGrid.RowCount - 1

            With UsersGrid
                If .Item(.Columns("User Name").Index, r).Value.ToString = UserName Then
                    .CurrentCell = .Item(.Columns("User Name").Index, r)
                End If
            End With
        Next

    End Sub

    Private Sub UsersGrid_CellEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles UsersGrid.CellEnter
        Job("Loading User Activities...")
        If UsersGrid.CurrentRow.Index = -1 Then
            UserSelectedLabel.Text = "No user selected"
        Else
            UserSelectedLabel.Text = UsersGrid.Item(0, UsersGrid.CurrentRow.Index).Value
            Load_Activities(UserSelectedLabel.Text)
        End If
        Job("Ready")
    End Sub

    Private Sub FilterButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FilterButton.Click

        If UserFilterDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            With UserFilterDialog
                If .NoFilterBox.Checked = True Then
                    Filter = False
                    FilterLabel.Text = "No Filter has been applied"
                    mainsql = "Select User_Name as [User Name], User_Level as [Level], Last_Logon as [Last Logon] from Users"
                Else
                    FilterLabel.Text = "The data has been filtered"
                    Filter = True

                    'Filter has been use so change sql statement
                    mainsql = "Select distinct u.User_Name as [User Name], User_Level as [Level], Last_Logon as [Last Logon] " & _
                                  "from Users u, User_Activities a " & _
                                  "where u.User_Name = a.User_name "
                    If .DatesCheckBox.Checked = True Then
                        mainsql = mainsql & "and (a.Activity_Date >= " & .FromBox.Value.Date.ToOADate - 2 & " and a.Activity_Date <= " & (.ToBox.Value.Date.ToOADate - 2) + 0.9999999999 & ") "

                    End If

                    If .AreaCheckBox.Checked = True Then
                        'change this later on  but just to keep it working...
                        Filter = False
                        FilterLabel.Text = "No Filter has been applied"
                        mainsql = "Select User_Name as [User Name], User_Level as [Level], Last_Logon as [Last Logon] from Users"
                    End If
                End If
            End With
            LoadUsersgrid()
            If UsersGrid.CurrentRow.Index = -1 Then
                UserSelectedLabel.Text = "No user selected"
            Else
                UserSelectedLabel.Text = UsersGrid.Item(0, UsersGrid.CurrentRow.Index).Value
                Load_Activities(UserSelectedLabel.Text)
            End If
        End If
    End Sub

    Private Sub UsersForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        If UserFormCheck(Me.Name) = "N" Then
            Me.Close()
            MessageBox.Show("Currently you do not have permission to access this form." & vbNewLine & _
                            "Contact TheStore.EFA@education.gsi.gov.uk if you need access.", Me.Name & " Access", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Exit Sub
        End If
    End Sub
End Class